//
//  KLChannelAreaViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLChannelAreaViewController.h"
#import "KLChannelXiangViewController.h"
@interface KLChannelAreaViewController ()
@property(nonatomic, strong) NSArray                     *xiangArray;
@end

@implementation KLChannelAreaViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.lastPlace;
    // Do any additional setup after loading the view.
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    NSDictionary *dic = self.dataSouce[indexPath.row];
    cell.textLabel.text = dic[@"name"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataSouce[indexPath.row];
    NSString * code = dic[@"code"];
    NSString * name = dic[@"name"];
    self.lastPlace = [NSString stringWithFormat:@"%@-%@",self.lastPlace,name];
    self.lastCode = [NSString stringWithFormat:@"%@-%@",self.lastCode,code];
    
    KLChannelViewController * vc;
    for (UIViewController * viewController in self.navigationController.childViewControllers) {
        if ([viewController isKindOfClass:[KLChannelViewController class]]) {
            vc = (KLChannelViewController*)viewController;
            if (vc.didChoseArea) {
                vc.didChoseArea(self.lastPlace, self.lastCode);
                NSInteger index = [self.navigationController.childViewControllers indexOfObject:vc];
                UIViewController * popVC = [self.navigationController.childViewControllers objectAtIndex:index - 1];
                [self.navigationController popToViewController:popVC animated:YES];
                break;
            }
        }
    }
  //  [self sendXiangWithRequestWithDic:dic];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

- (void)sendXiangWithRequestWithDic:(NSDictionary*)parmDic{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:@"3" forKey:@"type"];
    NSString * code = parmDic[@"code"];
    NSString * name = parmDic[@"name"];
    [dic setObject:code forKey:@"code"];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/user/address"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"resqust"]) {
                                                     self.xiangArray = [NSMutableArray new];
                                                     self.xiangArray = dataDic[@"resqust"];
                                                 }
                                                 if (self.xiangArray.count) {
                                                     KLChannelXiangViewController * vc= [KLChannelXiangViewController new];
                                                     vc.dataSouce = self.xiangArray;
                                                     vc.lastPlace = [NSString stringWithFormat:@"%@-%@",self.lastPlace,name];
                                                     vc.lastCode = [NSString stringWithFormat:@"%@-%@",self.lastCode,code];;
                                                     [self.navigationController pushViewController:vc animated:YES];
                                                 }else{
                                                     //支付
                                                     NSLog(@"支付渠道商");
                                                 }
                                                 [self.tableView reloadData];
                                                 [SVProgressHUD dismiss];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView reloadData];
                                                 
                                             }];
}

@end
